<?php

namespace App\Listeners;

use App\Events\MachineState;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Services\Machine\Setting as MachineSetting;
use App\Models\Machine;


class Setting
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  MachineState  $event
     * @return void
     */
    public function handle(MachineState $event)
    {   
        $imeis = $event->imeis;
        if(is_array($imeis) && !empty($imeis)){
           foreach ($imeis as $key => $value) {
                $machine = Machine::query()->with('config')->where('imei',$value)->find();
                if($machine){
                    (new MachineSetting)->store($machine);
                } 
            }            
        }        
    }
}
